package org.csu.agrisupply.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.agrisupply.api.common.CommonResponse;
import org.csu.agrisupply.api.entity.Form;
import org.csu.agrisupply.api.entity.Item;
import org.csu.agrisupply.api.entity.RC;
import org.csu.agrisupply.api.entity.User;
import org.csu.agrisupply.api.persistence.ItemMapper;
import org.csu.agrisupply.api.persistence.RCMapper;
import org.csu.agrisupply.api.persistence.UserMapper;
import org.csu.agrisupply.api.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("ItemService")
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RCMapper rcMapper;
    @Override
    public CommonResponse<List<Item>> getItemList() {
        List<Item> itemList = itemMapper.selectList(null);
        if(itemList.isEmpty()){
            return CommonResponse.createForSuccessMessages("没有分类信息");
        }
        return CommonResponse.createForSuccess(itemList);
    }

    @Override
    public CommonResponse<Item> getItem(String itemid) {
        Item item = itemMapper.selectById(itemid);
        if(item == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的item");
        }
        return  CommonResponse.createForSuccess(item);
    }

    @Override
    public CommonResponse<Item> RCputonItem( String rcId,Item item) {
        RC rc = rcMapper.selectById(rcId);
        if(rc == null){
            return CommonResponse.createForSuccessMessages("没有该ID的合作社");
        }
        item.setItemId(getMax());
        item.setRcId(rcId);
        itemMapper.insert(item);
        return CommonResponse.createForSuccess("上架成功",item);
    }

    @Override
    public CommonResponse<Item> RCupdateItem(String itemid, String rcId,Item newitem) {
        Item item = itemMapper.selectById(itemid);
        if(item == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的item");
        }
        RC rc = rcMapper.selectById(rcId);
        if(rc == null){
            return CommonResponse.createForSuccessMessages("没有该ID的合作社");
        }
        item = newitem;
        itemMapper.updateById(item);
        return CommonResponse.createForSuccess("修改成功",item);
    }

    @Override
    public CommonResponse<Item> RCputoffItem(String itemid, String rcId) {
        Item item = itemMapper.selectById(itemid);
        if(item == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的item");
        }
        RC rc = rcMapper.selectById(rcId);
        if(rc == null){
            return CommonResponse.createForSuccessMessages("没有该ID的合作社");
        }
        itemMapper.deleteById(itemid);
        return CommonResponse.createForSuccessMessages("下架成功");
    }

    @Override
    public CommonResponse<List<Item>> getRCitems(String rcId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rcId",rcId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return CommonResponse.createForSuccessMessages("没有分类信息");
        }
        return CommonResponse.createForSuccess(itemList);
    }

    public String getMax(){
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.select("itemId");
        List<Object> itemList = itemMapper.selectObjs(wrapper);
        int Max = 0;
        if(!itemList.isEmpty()){
            Max = Integer.parseInt(itemList.get(0).toString());
            for(Object item : itemList){
                int num = Integer.parseInt(item.toString());
                if (num >= Max)
                    Max = num;
            }
        }
        return Integer.toString(Max + 1);
    }
}
